package com.rritw.webtools.action.tool;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.util.HtmlUtils;

import com.googlecode.htmlcompressor.compressor.HtmlCompressor;
import com.rritw.webtools.action.IpsAction;
import com.rritw.webtools.util.HttpClientUtil;
import com.rritw.webtools.util.StringUtil;

@Controller
@Scope("prototype")
public class HtmlCompressAction extends IpsAction {

    private static final long   serialVersionUID = 7703819655895958883L;
    private static final Logger log              = Logger.getLogger(HtmlCompressAction.class);
    private String              url;
    private String              content;
    private String              requestType;
    private String              contentResult;

    public String index() {
        super.init();
        requestType = "url";
        return SUCCESS;
    }

    public String create() {
        super.init();
        if (StringUtil.isEmpty(requestType)) {
            log.warn("action is empty.");
            return ERROR;
        }
        if (StringUtil.isEmpty(content) && StringUtil.isEmpty(url)) {
            log.warn("Content and url are both empty.");
            return ERROR;
        }
        try {
            if ("url".equals(requestType)) {
                if (!StringUtil.isEmpty(url)) {
                    content = HttpClientUtil.doGet(url).getContent();
                } else {
                    log.warn("The request type is url, but the is empty.");
                    return ERROR;
                }
            }
            HtmlCompressor compressor = new HtmlCompressor();
            compressor.setCompressCss(true); // compress inline css
            compressor.setCompressJavaScript(true); // compress inline javascript
            compressor.setYuiCssLineBreak(80); // --line-break param for Yahoo YUI Compressor
            compressor.setYuiJsDisableOptimizations(true); // --disable-optimizations param for Yahoo YUI Compressor
            compressor.setYuiJsLineBreak(-1); // --line-break param for Yahoo YUI Compressor
            compressor.setYuiJsNoMunge(true); // --nomunge param for Yahoo YUI Compressor
            compressor.setYuiJsPreserveAllSemiColons(true);// --preserve-semi param for Yahoo YUI Compressor
            compressor.setRemoveComments(true);// Remove comments
            contentResult = compressor.compress(content);
            if (!StringUtil.isEmpty(content)) {
                content = HtmlUtils.htmlEscape(content);
            }
            if (!StringUtil.isEmpty(contentResult)) {
                contentResult = HtmlUtils.htmlEscape(contentResult);
            }
            return SUCCESS;
        } catch (Exception e) {
            log.error("Compress HTML exception happened:" + e.getMessage(), e);
            return ERROR;
        }
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getRequestType() {
        return requestType;
    }

    public void setRequestType(String requestType) {
        this.requestType = requestType;
    }

    public String getContentResult() {
        return contentResult;
    }

}
